package top.shenluw.springdemo.dubbo.provider;

import org.apache.commons.io.FileUtils;
import org.apache.zookeeper.server.NIOServerCnxnFactory;
import org.apache.zookeeper.server.ZooKeeperServer;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * @author shenluw
 * @date 2022/4/18 16:47
 */
@SpringBootApplication
public class Application {


	static {
		try {
			Path snapDir = Files.createTempDirectory("zksnap");

			Path logDir = Files.createTempDirectory("zklog");
			ZooKeeperServer zookeeper = new ZooKeeperServer(snapDir.toFile(), logDir.toFile(), 500);
			NIOServerCnxnFactory factory = new NIOServerCnxnFactory();
			factory.configure(new InetSocketAddress("127.0.0.1", 2181), 0);
			factory.startup(zookeeper);

			Runtime.getRuntime().addShutdownHook(new Thread(() -> {
				zookeeper.shutdown(true);
				factory.shutdown();
				try {
					FileUtils.deleteDirectory(snapDir.toFile());
					FileUtils.deleteDirectory(logDir.toFile());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}));
		} catch (IOException | InterruptedException e) {
			throw new RuntimeException(e);
		}
	}
}
